
#include "public_msg.h"

static  msg_packet_t  system_msg;

int message_packet_init(void)
{
	int i = 0;

	for(i=0;i<MAX_MSG_SIZE_4K;i++)
	{
		system_msg.msg_box[i].msg_cmd   = 0; 
		system_msg.msg_box[i].msg_data0 = 0;
		system_msg.msg_box[i].msg_data1 = 0;
	}

	system_msg.msg_rindex = 0; 
	system_msg.msg_windex = 0;

	return 0;
}

int message_packet_send(unsigned int cmd,unsigned int data0,unsigned int data1)
{

	system_msg.msg_box[system_msg.msg_windex].msg_cmd	  = cmd; 
	system_msg.msg_box[system_msg.msg_windex].msg_data0   = data0;
	system_msg.msg_box[system_msg.msg_windex].msg_data1   = data1;

	system_msg.msg_windex++;
	if(system_msg.msg_windex >= MAX_MSG_SIZE_4K)
	{
		system_msg.msg_windex = 0;	
	}

	return 0;

}


int message_packet_recv(unsigned int *cmd,unsigned int *data0,unsigned int *data1)
{	
	if(!cmd)
		return -1;
	
	if(!data0)
		return -1;	

	if(!data1)
		return -1;

	*cmd   = 0;
	*data0 = 0;
	*data1 = 0;

	if(system_msg.msg_windex == system_msg.msg_rindex)
	{
		return -1;
	}

	*cmd   = system_msg.msg_box[system_msg.msg_rindex].msg_cmd;
	*data0 = system_msg.msg_box[system_msg.msg_rindex].msg_data0;
	*data1 = system_msg.msg_box[system_msg.msg_rindex].msg_data1;

	system_msg.msg_rindex++;
	if(system_msg.msg_rindex >= MAX_MSG_SIZE_4K)
	{
		system_msg.msg_rindex = 0;	
	}

	return 0;
}

int message_packet_clear(void)
{
	int i = 0;

	for(i=0;i<MAX_MSG_SIZE_4K;i++)
	{
		system_msg.msg_box[i].msg_cmd   = 0; 
		system_msg.msg_box[i].msg_data0 = 0;
		system_msg.msg_box[i].msg_data1 = 0;
	}

	system_msg.msg_rindex = 0; 
	system_msg.msg_windex = 0;

	return 0;
}


int message_packet_deinit(void)
{
	int i = 0;

	for(i=0;i<MAX_MSG_SIZE_4K;i++)
	{
		system_msg.msg_box[i].msg_cmd   = 0; 
		system_msg.msg_box[i].msg_data0 = 0;
		system_msg.msg_box[i].msg_data1 = 0;
	}

	system_msg.msg_rindex = 0; 
	system_msg.msg_windex = 0;

	return 0;
}













